package com.sx.pipeline.ui.task.participate.frag

import com.blankj.utilcode.util.SPUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.noober.background.view.BLTextView
import com.sx.pipeline.R
import com.sx.pipeline.app.config.IdentityType
import com.sx.pipeline.app.config.ReleaseTaskType
import com.sx.pipeline.app.config.SPKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.repos.http.res.ReleaseTaskVo
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO

/**
 * @Author：YY
 * @Date：2022/2/10
 * @Describe: 任务列表适配器
 */
class ParticipateTaskListAdapter(
) : BaseQuickAdapter<ReleaseTaskVo, BaseViewHolder>(
    R.layout.item_operating_unit_release
) {
    /**
     * 删除任务
     */
    var deleteTaskCallback: ((Int) -> Unit)? = null

    /**
     * 加入群聊
     */
    var joinGroupCallback: ((Int,String) -> Unit)? = null
    override fun convert(holder: BaseViewHolder, item: ReleaseTaskVo) {
        //工作性质
        holder.setText(R.id.tv_nature,item.nature)
        //任务时间
        holder.setText(R.id.tv_start_time,item.startTime)
        //任务标题
        holder.setText(R.id.tv_title,item.title)
        //项目名称
        holder.setText(R.id.tv_project_name,"项目名称："+item.name)
        //工作量
        holder.setText(R.id.tv_work_load,"工作量："+item.workload+(item.unit ?: "米"))
        //施工地点
        holder.setText(R.id.tv_release_address,"施工地点："+item.addressDetail)
        //现场负责人
        holder.setText(R.id.tv_release_principal,"现场负责人："+item.contact)

        //任务状态
        when(item.status){
            ReleaseTaskType.AGENCY.code->holder.setText(R.id.tv_status,R.string.txt_upcoming)
            ReleaseTaskType.IN_PROGRESS.code->holder.setText(R.id.tv_status,R.string.txt_in_progress)
            ReleaseTaskType.COMMENT.code->holder.setText(R.id.tv_status,R.string.txt_be_evaluated)
            ReleaseTaskType.COMPLETED.code->holder.setText(R.id.tv_status,R.string.txt_completed)
            ReleaseTaskType.OVER.code->holder.setText(R.id.tv_status,R.string.txt_over)
            ReleaseTaskType.REJECTED.code->holder.setText(R.id.tv_status,R.string.txt_rejected)
        }
        when (item.status) {
            //待办
            1 -> {
                holder.setText(R.id.tv_action, context.getString(R.string.txt_processing_tasks))
                holder.setText(R.id.tv_delete, context.getString(R.string.txt_join_group))
                holder.setGone(R.id.tv_action,
                    SPUtils.getInstance().getInt(SPKey.SP_IDENTITY_TYPE, 0)== IdentityType.OPERATING_UNIT.code)
                holder.setVisible(R.id.tv_delete,true)
                //有群聊 则显示加入群聊按钮
                if (item.ryGroupId!=null){
                    holder.setText(R.id.tv_delete, context.getString(R.string.txt_join_group))
                    holder.setVisible(R.id.tv_delete,true)
                }else{
                    holder.setGone(R.id.tv_delete,true)
                }
            }
            //在办
            2 -> {
                holder.setGone(R.id.tv_action,true)
                holder.setText(R.id.tv_delete, context.getString(R.string.txt_join_group))
            }
            //待评价
            3 -> {
                holder.setGone(R.id.tv_action,true)
                holder.setGone(R.id.tv_delete,true)
            }
            //已完成
            4 -> {
                holder.setText(R.id.tv_delete, context.getString(R.string.txt_delete_task))
                holder.setVisible(R.id.tv_delete, true)

                if (item.evaluate){
                    holder.setText(R.id.tv_action, context.getString(R.string.txt_view_reviews))
                    holder.setVisible(R.id.tv_action,true)
                }
            }
            //已结束
           5 -> {
                holder.setText(R.id.tv_delete, context.getString(R.string.txt_delete_task))
                holder.setVisible(R.id.tv_delete, true)
                holder.setGone(R.id.tv_action, true)
            }
        }

        holder.getView<BLTextView>(R.id.tv_action).setOnClickListener {
            when (item.status) {
                //处理任务
                ReleaseTaskType.AGENCY.code -> RouteManager.Mine.intentToTaskProcess(item.id,
                    item?.hasGroup.toString()
                )
                //查看评价
                ReleaseTaskType.COMPLETED.code ->RouteManager.Mine.intentToEvaluationList(item.id)

            }
        }
        holder.getView<BLTextView>(R.id.tv_delete).setOnClickListener {
            when (item.status) {
                //删除任务
                4, 5 -> deleteTaskCallback?.invoke(item.id)
                1,2-> {
                    //加入群聊
                    item.ryGroupId?.let { it1 -> joinGroupCallback?.invoke(item.id, it1) }
                }
            }
        }
    }
}